<?php

namespace Lib\Util;

class Util {

    public static function debug($var) {
        echo '<pre class="debug">' . PHP_EOL;
        if (empty($var))
            var_dump($var);
        else
            print_r($var);
        echo PHP_EOL.'</pre>';
    }

    public static function parseFileSize($size) {
        preg_match('/[a-z]$/i', $size, $unit);
        if (! empty($unit)) {
            $unit = strtolower($unit[0]);

            $size = (float)$size;
            switch($unit) {
                case 'g':
                    $size *= 1000000000;
                    break;

                case 'm':
                    $size *= 1000000;
                    break;

                case 'k':
                    $size *= 1000;
                    break;
            }
        }

        return (float)$size;
    }

    public static function minify($html) {
        $search = array(
            '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
            '/[^\S ]+\</s',  // strip whitespaces before tags, except space
            '/(\s)+/s'       // shorten multiple whitespace sequences
        );

        $replace = array(
            '>',
            '<',
            '\\1'
        );

        $html = preg_replace($search, $replace, $html);

        return $html;
    }
}